package com.ren.service.oss.rest;

import com.ren.common.exception.BusinessException;
import com.ren.common.result.R;
import com.ren.common.result.ResponseEnum;
import com.ren.service.oss.service.IOssFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * @author RenGaoshuai
 * @date 2023/8/8 9:40
 * @description
 */
@RequestMapping("/admin/oss")
@Slf4j
@RestController
public class OssFileUploadController {

    @Autowired
    private IOssFileService ossFileService;

    @PostMapping("/uploadOss")
    public R uploadOss(@RequestParam("file") MultipartFile file,
                       @RequestParam("module") String module) {

        try {
            InputStream inputStream = file.getInputStream();
            String originalFilename = file.getOriginalFilename();
            String uploadUrl = ossFileService.upload(inputStream, module, originalFilename);
            //返回r对象
            return R.ok().message("文件上传成功").data("url", uploadUrl);
        } catch (Exception e) {
            throw new BusinessException(ResponseEnum.UPLOAD_ERROR);
        }
    }

}
